﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityNetwork;
using System.Net;
using System.Net.Sockets;
using System;
using System.IO;

public class GameNetHandler : MyEventHandler
{
    private TCPPeer peer;
    private Socket sock = null;

    [Serializable]
    public class NetMsg
    {
        public short msgId;
        public string msg;
    }


    public GameNetHandler()
    {
        peer = new TCPPeer(this);
    }

    public void ConnectServer(string ip,int port)
    {
        sock = peer.Connect(ip, port);
    }

    public void Send(short id, byte[] buffer)
    {
        Packet p = new Packet((short)SocketManager.GameMessageId.NetMsg);
        p.EncodeBuffer(id, buffer);
        TCPPeer.Send(sock, p);
    }

    public void Close()
    {
        try
        {
            if (sock != null)
            {
                sock.Close();
                sock = null;
            }
        }
        catch (Exception e)
        {
            Debug.Log(e.Message);
        }
    }
}
